package com.inspur.cloud.dao.dataspace;

import com.inspur.cloud.entity.dataspace.HdfsResourceEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface HdfsResourceDao extends JpaRepository<HdfsResourceEntity, Integer>, JpaSpecificationExecutor<HdfsResourceEntity> {
    HdfsResourceEntity findFirstByUrl(String path);

    @Query("select h from HdfsResourceEntity h where h.owner=:owner and h.parentId=:parentId")
    HdfsResourceEntity findFirstByOwner(@Param("owner") Integer owner, @Param("parentId") Integer parentId);

    /**
     * 找到所有儿子节点
     *
     * @param parentId
     * @return
     */
    List<HdfsResourceEntity> findByParentId(Integer parentId);

    List<Integer> findIdByParentId(Integer parentId);

    @Query("delete from HdfsResourceEntity h where h.parentId=:parentId")
    @Modifying
    Integer deleteByParentId(@Param("parentId") Integer parentId);

    @Query("select h.id from HdfsResourceEntity h where h.url like ?1%")
    List<Integer> findByUrlLike(@Param("url") String url);

    @Query("delete from HdfsResourceEntity h where h.id in :idList")
    @Modifying
    Integer deleteByIdList(@Param("idList") List<Integer> idList);

    HdfsResourceEntity findHdfsResourceEntityById(Integer id);
}
